package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: commentController类的描述
 * @Author: wuyuanhao
 * @CreateDate: 2020/8/13 10:22
 * @UpdateUser: wuyuanhao
 * @UpdateDate: 2020/8/13 10:22
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Controller
public class commentController {
    /** 头像保存路径 */
    public static final String WINDOWS_PROFILES_PATH = "C:/super_meeting/profiles/";
    public static final String LINUX_PROFILES_PATH = "/root/super_meeting/profiles/";
    @Autowired
    public UserService userService;

    /**
     * 上传event logo
     * @param profile
     * @param request
     */
    @PutMapping("/profiles")
    @ResponseBody
    public String uploadEventLogo(@RequestParam(required = true) MultipartFile profile, HttpServletRequest request) {
//        String uploadPath=System.getProperty("user.dir")+"\\src\\main\\resources\\static\\upload";
        String uploadPath=System.getProperty("user.dir")+"\\target\\classes\\static\\upload";
        String str=userService.saveFile(profile,uploadPath);
        return str;
    }

        //    @ApiOperation(value = "设置用户头像", notes = "设置当前用户头像")
//    @PutMapping("/profiles")
//    public Map<String,Object> setUserProfile(@RequestParam(required = true) MultipartFile profile, HttpSession session) {
//        Map<String,Object> map=new HashMap<String,Object>();
//        map.put("status",HttpStatus.OK.value());
//        // 根据Windows和Linux配置不同的头像保存路径
//        String OSName = System.getProperty("os.name");
//        String profilesPath = OSName.toLowerCase().startsWith("win") ? WINDOWS_PROFILES_PATH : LINUX_PROFILES_PATH;
//        if (!profile.isEmpty()) {
//            // 当前用户
//            User currentUser = (User) session.getAttribute("user");
//            String profilePathAndNameDB =currentUser.getProfile();
//            // 默认以原来的头像名称为新头像的名称，这样可以直接替换掉文件夹中对应的旧头像
//            String newProfileName = profilePathAndNameDB;
//            // 若头像名称不存在
//            if (profilePathAndNameDB == null || "".equals(profilePathAndNameDB)) {
//                newProfileName = profilesPath+ System.currentTimeMillis()+ profile.getOriginalFilename();
//                // 路径存库
//                currentUser.setProfile(newProfileName);
//                userService.updateUser(currentUser);
//            }
//            // 磁盘保存
//            BufferedOutputStream out = null;
//            try {
//                File folder = new File(profilesPath);
//                if (!folder.exists())
//                    folder.mkdirs();
//                out = new BufferedOutputStream(new FileOutputStream(newProfileName));
//                // 写入新文件
//                out.write(profile.getBytes());
//                out.flush();
//            } catch (Exception e) {
//                e.printStackTrace();
//                map.put("msg","设置头像失败");
//                map.put("bool",Boolean.FALSE);
//                return map;
//            } finally {
//                try {
//                    out.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            map.put("msg","设置头像成功");
//            map.put("bool",Boolean.TRUE);
//            return map;
//        } else {
//            map.put("msg","设置头像失败");
//            map.put("bool",Boolean.FALSE);
//            return map;
//        }
//    }
}
